﻿using System;
using System.Threading.Tasks;
using System.Web.Mvc;
using Amba.ULogin.Models;
using Amba.ULogin.Services;
using Orchard;
using Orchard.Localization;
using Orchard.UI.Admin;
using Orchard.UI.Notify;

namespace Amba.ULogin.Controllers
{
    public class SettingsViewModel
    {
        public bool SaveUserPhotoToDisk { get; set; }

        public SettingsViewModel() { }

        public SettingsViewModel(ULoginSettingsRecord settings)
        {
            SaveUserPhotoToDisk = settings.SaveUserPhotoToDisk;
        }
    }

    [Admin]
    public class AdminController : Controller
    {
        private readonly IULoginServiceSettings _settingsService;

        public AdminController(
            IULoginServiceSettings settingsService)
        {
            _settingsService = settingsService;
        }

        public ActionResult Settings()
        {
            var viewModel = new SettingsViewModel(_settingsService.Settings);
            return View(viewModel);
        }       

        [HttpPost]
        public ActionResult Settings(SettingsViewModel viewModel)
        {
            if (!ModelState.IsValid)
                return View(viewModel);
            _settingsService.Settings.SaveUserPhotoToDisk = viewModel.SaveUserPhotoToDisk;
            _settingsService.Save();
            return RedirectToAction("Settings");
        }
    }
}